#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === File Uploader Wrapper ===
function fileupl()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: fileupl <file_path> [target_folder] (File to release server uploader)';
    echo '';
    return;
  fi;

  # Launch file uploader
  if shtools-uploads-ftp-active; then
    uploadftp "${1}" "${2}";
  fi;
  if shtools-uploads-mega-active; then
    uploadmega "${1}" "${2}";
  fi;
  if shtools-uploads-basketbuild-active; then
    uploadbasketbuild "${1}" "${2}";
  fi;
}

# === File Downloader Wrapper ===
function fileget()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: fileget <remote_path> [boot_remove_remote] (File from release server downloader)';
    echo '';
    return;
  fi;

  # Launch file downloader
  if shtools-uploads-ftp-active; then
    downloadftp "${1}" "${2}";
  fi;
  if shtools-uploads-mega-active; then
    downloadmega "${1}" "${2}";
  fi;
  if shtools-uploads-basketbuild-active; then
    downloadbasketbuild "${1}" "${2}";
  fi;
}

# === Fast File Uploader ===
function fastupl()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: fastupl <file_path> [none/zip/bootimage] (Fast private file to server uploader)';
    echo '';
    return;
  fi;

  # Variables
  local apkname;
  local filename;
  local filepath;
  local cmd;

  # Initialize variables
  filename=$(basename "${1}");

  # Launch file private uploader
  if shtools-uploads-mega-active; then
    uploadprivatemega "${1}" "${2}";
    cmd="downloadmega '/Root/Private/${filename}';";
  elif shtools-uploads-basketbuild-active; then
    uploadprivatebasketbuild "${1}" "${2}";
    cmd="downloadbasketbuild '/Root/Private/${filename}';";
  fi;

  # Abort if no command provided
  if [ -z "${cmd}" ]; then
    echo '';
    return;
  fi;

  # Additional install commands
  case "${2}" in

    # Flashable zip
    zip)
      cmd="${cmd} adbside '${filename}' true;";
      ;;

    # Bootimage file
    bootimage)
      cmd="${cmd} fboot '${filename}';";
      ;;

    # Generic case
    *)
      # /system/ file
      if [[ "${1}" == *'/system/'* ]]; then
        filepath=/system/${1#*/system/};
        cmd="${cmd} adbpu '${filename}' '${filepath}';";

        # .apk application
        if [ "${1: -4}" = '.apk' ]; then
          apkname=$(aapt d xmltree "${1}" AndroidManifest.xml | grep -i 'package=');
          apkname=${apkname#*$'"'};
          apkname=${apkname%%$'"'*};
          if [ ! -z "${apkname}" ]; then
            cmd="${cmd} adbu '${apkname}';";
          fi;
        fi;
      fi;
      ;;

  esac;

  # Command outputs
  echo -e '  \e[1;32mDirect download:\e[0m';
  echo "${cmd}" | xclip -selection c 2> /dev/null;
  echo "   ${cmd}";
  echo '';
}
